using Assets.Scripts.UI;
using Photon.Realtime;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;
using HashTable = ExitGames.Client.Photon.Hashtable;


public class UIMain_Panel : MonoBehaviourPunCallbacks
{
    public static UIMain_Panel Instance;

    public GameObject CardPrefab;   //手牌item模板
    public Transform CardItemParent;    //手牌区域的位置

    private void Awake()
    {
        Instance = this;
        Confirm_Button.onClick.AddListener(Confirm_ButtonClick);
        Cancel_Button.onClick.AddListener(Cancel_ButtonClick);
    }


    #region 牌库
    public Text StandByCard_All_Text;
    public Text StandByCard_Enemy_Text;
    public Text StandByCard_Player_Text;
    public void UpdateStandByCard_All(int num)
    {
        StandByCard_All_Text.text = "X" + num;
    }
    public void UpdateStandByCard_Enemy(int num)
    {
        StandByCard_Enemy_Text.text = num.ToString();
    }
    public void UpdateStandByCard_Player(int num)
    {
        StandByCard_Player_Text.text = num.ToString();
    }

    #endregion


    #region 生命值
    public Text Enemy_HP_Text;
    public Text Player_HP_Text;

    public void UpdateEnemy_HP(int hp)
    {
        Enemy_HP_Text.text = hp.ToString();
    }
    public void UpdatePlayer_HP(int hp)
    {
        Player_HP_Text.text = hp.ToString();
    }
    #endregion


    #region 交互按钮
    public Button Confirm_Button;
    public Button Cancel_Button;
    public Sprite Confirm_Button_Disable;
    public Sprite Confirm_Button_Normal;
    public Image TimeImage;

    /// <summary>
    /// 确认出牌 按钮点击
    /// </summary>
    private void Confirm_ButtonClick()
    {
        // 每次玩家点击卡片后，都会检查canUseCard
        // 当前卡如果可以使用
        if (canUseCard)
        {
            Player.Instance.Confirm_ButtonClick();
        }
    }

    /// <summary>
    /// 取消出牌 按钮点击
    /// </summary>
    private void Cancel_ButtonClick()
    {
        Player.Instance.Cancel_ButtonClick();
    }

    public void UpdateTime(float value)
    {
        TimeImage.fillAmount = value;
    }

    #endregion


    #region 出牌效果
    //TODO: 将来要根据玩家的阵营来显示在出牌区的不同位置，红方靠左，蓝方靠右

    public Image UserCardEF_Image;
    //public AudioSource AudioSource;

    /// <summary>
    /// 使用卡 效果
    /// </summary>
    public void UserCard_Effect(CardDefine card)
    {
        // 卡牌音效
        //AudioSource.PlayOneShot(card.useAudioClip);
        // 卡牌视觉效果
        UserCardEF_Image.sprite = card.cardImage;
        StopCoroutine(DoCardImageEffect());
        StartCoroutine(DoCardImageEffect());
    }

    IEnumerator DoCardImageEffect()
    {
        UserCardEF_Image.color = Color.white;
        Color color = Color.white;
        while (color.a > 0)
        {
            color.a -= Time.deltaTime;
            UserCardEF_Image.color = color;
            yield return null;
        }
        UserCardEF_Image.color = new Color(1, 1, 1, 0);
    }

    #endregion


    #region 玩家交互

    #region 用于临时存放手牌item
    // 当前玩家选择的卡-可能会出
    private int currCardID = -1;
    // 用ID去确认某个UICardItem
    private Dictionary<int, UICard_Item> cardItemDic = new Dictionary<int, UICard_Item>();
    #endregion




    #region 卡牌的选择与使用
    // 能否使用当前卡
    private bool canUseCard;
    public bool CanUseCard
    {
        get => canUseCard;
        set
        {
            canUseCard = value;
            if (canUseCard)
            {
                // 让确认按钮显示高亮状态
                Confirm_Button.image.sprite = Confirm_Button_Normal;
            }
            else
            {
                Confirm_Button.image.sprite = Confirm_Button_Disable;
            }
        }
    }

    /// <summary>
    /// 选中的手牌上的卡
    /// </summary>
    public void SelectCard(int cardID)
    {
        // 如果上一次选择和本次是一样的，就无视
        if (currCardID == cardID)
        {
            return;
        }
        // 找到老的ID对应的实际UIItem元素，设置他的Select为False
        if (cardItemDic.ContainsKey(currCardID))
        {
            cardItemDic[currCardID].IsSelect = false;
        }

        currCardID = cardID;
        cardItemDic[currCardID].IsSelect = true;

        // 让Player脚本知道当前选择的卡
        Player.Instance.currSelectCardID = currCardID;
        // 根据玩家是否可以使用此卡，来决定按钮的显示状态
        CanUseCard = Player.Instance.CheckCard(cardID);
    }


    #endregion




    #region 卡牌的添加与删除
    /// <summary>
    /// 添加一张卡到UI的面板中
    /// </summary>
    public void AddCard(int cardID, CardDefine card)
    {
        UICard_Item cardItem = Instantiate(CardPrefab, CardItemParent).GetComponent<UICard_Item>();
        cardItem.Init(cardID, card);
        cardItemDic.Add(cardID, cardItem);
    }

    /// <summary>
    /// 删除UI上某个卡
    /// </summary>
    /// <param name="cardID"></param>
    public void RemoveCard(int cardID)
    {
        Destroy(cardItemDic[cardID].gameObject);
        cardItemDic.Remove(cardID);
    }

    /// <summary>
    /// 移除UI上的所有卡片
    /// </summary>
    public void RemoveAllCard()
    {
        foreach (UICard_Item item in cardItemDic.Values)
        {
            Destroy(item.gameObject);
        }
        cardItemDic.Clear();
    }
    #endregion


    /// <summary>
    /// 当前是否是我的回合
    /// </summary>
    private bool isMyTurn;
    public bool IsMyTurn
    {
        get => isMyTurn;
        set
        {
            isMyTurn = value;
            // 打开或隐藏按钮
            Confirm_Button.gameObject.SetActive(isMyTurn);
            Cancel_Button.gameObject.SetActive(isMyTurn);
            // 打开或隐藏剩余时间
            TimeImage.gameObject.SetActive(isMyTurn);

            // 如果是当前回合
            if (isMyTurn)
            {

            }
            // 不是当前回合
            else
            {
                // 取消之前可能选择的卡
                if (cardItemDic.ContainsKey(currCardID))
                {
                    cardItemDic[currCardID].IsSelect = false;
                }
            }
        }
    }
    #endregion

    #region 网络相关
    public HashTable Gaming = new HashTable();  // 创建一个游戏中的哈希表，用来管理在游戏过程中需要进行网络传递的字段

    private void Start()
    {
        Gaming.Add("IsMyTurn", IsMyTurn);

        if (PhotonNetwork.LocalPlayer.IsMasterClient)   // 房主（红方）是后手出牌
        {
            IsMyTurn = false;
        }
        else
        {
            IsMyTurn = true;
        }
    }
    public override void OnPlayerPropertiesUpdate(Photon.Realtime.Player targetPlayer, HashTable changedProps)
    {
        if (targetPlayer != PhotonNetwork.LocalPlayer)  // 更新对方的数据（注意不是更新自己的而是对方的！！）
        {
            IsMyTurn = !(bool)changedProps["IsMyTurn"];
        }
    }
    #endregion
}
